package leetcode;

import java.util.HashMap;
import java.util.Map;
/*
 * 解析前一行数字
 * */
public class Day7 {
    public String countAndSay(int n) {
        String ret="1";
        for (int i=1;i<n;i++){
            StringBuilder stringBuilder=new StringBuilder();
            int len=ret.length();
            for (int left=0,right=0;right<len;){
                while (right<len&&ret.charAt(left)==ret.charAt(right)){
                    right++;
                }
                stringBuilder.append(ret.charAt(right-left));
                stringBuilder.append(ret.charAt(left));
                left=right;
            }
            ret=stringBuilder.toString();
        }
        return ret;
    }
    /*
     * 数青蛙
     * */

    public int minNumberOfFrogs(String croakOfFrogs) {
        char[] arr=croakOfFrogs.toCharArray();
        String s="croak";
        int l=s.length();
        int[] ret=new int[l];
        Map<Character,Integer> map=new HashMap<>();
        for (int i=0;i<l;i++){
            map.put(s.charAt(i),i);
        }
        for (char ch:arr){
            if(ch==s.charAt(0)){
                if(ret[l-1]!=0) ret[l-1]--;
                ret[0]++;
            }else {
                if(ret[map.get(ch)-1]==0){
                    return -1;
                }else {
                    ret[map.get(ch)-1]--;
                    ret[map.get(ch)]++;
                }
            }
        }
        for (int i=0;i<l-1;i++){
            if(ret[i]!=0){
                return -1;
            }
        }
        return ret[l-1];
    }
}
